package w;

import java.util.LinkedList;
import java.util.Queue;

//队列实现栈
class MyStackUsQueue{
    Queue<Integer> queue1;
    Queue<Integer> queue2;

    public MyStackUsQueue(Queue<Integer> queue1, Queue<Integer> queue2) {
         this.queue1=new LinkedList<>();
         this.queue2 = new LinkedList<>();
    }
    public void push(int x){
        if (isEmpty()){
            queue1.offer(x);
            return;
        }
        if (!queue1.isEmpty()){
            queue1.offer(x);
        }else {
            queue2.offer(x);
        }

    }
    public int pop(){
        if (isEmpty()){
            return -1;
        }
        if (!queue1.isEmpty()){
            int size=queue1.size();
            for (int i = 0; i < size-1; i++) {
                queue2.offer(queue1.poll());
            }
            return queue1.poll();
        }else {
            int size=queue2.size();
            for (int i = 0; i < size-1; i++) {
                queue1.offer(queue2.poll());
            }
            return queue2.poll();
        }
    }
    public int top(){
        if (isEmpty()){
            return -1;
        }
        if (!queue1.isEmpty()){
            int size=queue1.size();
            int ret=-1;
            for (int i = 0; i < size-1; i++) {
                ret=queue1.poll();
                queue2.offer(ret);
            }
            return ret;
        }else {
            int size=queue2.size();
            int ret=-1;
            for (int i = 0; i < size-1; i++) {
                ret=queue2.poll();
                queue1.offer(ret);
            }
            return ret;
        }
    }
    public boolean isEmpty(){
        return queue1.isEmpty()&&queue2.isEmpty();
    }
}
public class Text {


}
